/**
 * Created by GSY on 16/8/17.
 */

var hammerMask;
var hammerBg;
var hammerBox;
var hammerPage;
var imgBig;
var hammerImgs = [];
var ticking = false;
var transform = {scale: 1, lastScale: 1, top: 0, left: 0, lastTop:0, lastLeft:0};


window.initHammer = function (mask,bg,box,page,img) {
  hammerMask = mask;
  hammerBg = bg;
  hammerBox = box;
  hammerPage = page;
  imgBig = img;



  //hammer init
  var mc = new Hammer.Manager(hammerBox);
  mc.add(new Hammer.Pan({ threshold: 10, pointers: 0 }));
//    mc.add(new Hammer.Swipe()).recognizeWith(mc.get('pan'));
  mc.add(new Hammer.Pinch({ threshold: 0 }));
  mc.add(new Hammer.Tap({ event: 'doubletap', taps: 2 }));

//hammer 监听
  mc.on('pan panend', function(ev) {
    transform.top = transform.lastTop + ev.deltaY;
    transform.left = transform.lastLeft + ev.deltaX;

    // console.log('top '+transform.top+' left'+transform.left);

    if(ev.type == 'panend') {
      if(Math.abs(transform.top) > shiftHeight) {
        if(transform.top > 0) {
          transform.top = shiftHeight;
        } else {
          transform.top = -shiftHeight;
        }
      }
      if(Math.abs(transform.left) > shiftWidth) {
        if(transform.left > 0) {
          transform.left = shiftWidth;
        } else {
          transform.left = -shiftWidth;
        }
      }
      transform.lastTop = transform.top;
      transform.lastLeft = transform.left;
      if(transform.scale <= 1) {
        transform.lastLeft = 0;
        transform.lastTop = 0;
      }
    }
    ticking = false;
    requestElementUpdate();
  });
//    mc.on('swipe', function(ev) {
//      if(ev.deltaX < 0 && (currentIndex < imgNumIndex)) {
//        //next
//        nextImg();
//      }  else if (ev.deltaX > 0 && (currentIndex > 0)){
//        //last
//        lastImg();
//      }
//
//    });
  var shiftWidth = 0;
  var shiftHeight = 0;
  mc.on('pinchstart pinchmove pinchend',function (ev) {
    //pinch scale
    if(ev.type == 'pinchstart') {
      shiftWidth = 0;
      shiftHeight = 0;
    } else if(ev.type == 'pinchmove') {
      transform.scale = transform.lastScale * ev.scale;
      if(transform.scale > 1) {
        shiftWidth = (transform.scale-1)/2*imgBig.offsetWidth;
        shiftHeight = (transform.scale-1)/2*imgBig.offsetHeight;
      }
      ticking = false;
      requestElementUpdate();

    } else if (ev.type == 'pinchend') {

      transform.scale = transform.lastScale * ev.scale;
      if(transform.scale > 0.7 && transform.scale < 1.3) {
        transform.scale = 1;
        transform.top = 0;
        transform.left = 0;
      }
      if(transform.scale > 3) {
        transform.scale = 3;
      }
      if(transform.scale < 0.3) {
        transform.scale = 0.3;
      }
      transform.lastScale = transform.scale;
      ticking = false;
      requestElementUpdate();
    }

  });
  mc.on('doubletap', function(ev) {

    if(transform.scale < 3 && transform.scale >= 1) {
      transform.scale = 3;
      transform.lastScale = 3;
      shiftWidth = (transform.scale-1)/2*imgBig.offsetWidth;
      shiftHeight = (transform.scale-1)/2*imgBig.offsetHeight;
    } else {
      transform.scale = 1;
      transform.lastScale = 1;
      transform.lastLeft = 0;
      transform.lastTop = 0;
      transform.left = 0;
      transform.top = 0;
      shiftWidth = 0;
      shiftHeight = 0;
    }
    ticking = false;
    requestElementUpdate();
  });
//hammer 监听结束
}

//hammer animation
function requestElementUpdate() {
  if(!ticking) {
    ticking = true;
    var value = [
      'translate('+transform.left+'px,'+transform.top+'px)',
      'scale(' + transform.scale + ', ' + transform.scale + ')'
    ];

    value = value.join(" ");
    imgBig.style.webkitTransform = value;
    imgBig.style.transform = value;

    setTimeout(function() {
      ticking = false;
    },17);

  }

}

var currentIndex = 0;
var imgNumIndex = 0;

function lastImg() {
    if(currentIndex > 0) {
        currentIndex--;
        imgBig.src = "";
        imgBig.src = hammerImgs[currentIndex];
        hammerPage.innerHTML = (currentIndex+1)+"/"+(imgNumIndex+1);
    }
}
function nextImg() {
    if(currentIndex < imgNumIndex) {
        currentIndex++;
        imgBig.src = "";
        imgBig.src = hammerImgs[currentIndex];
        hammerPage.innerHTML = (currentIndex+1)+"/"+(imgNumIndex+1);
    }
}

window.showHammer = function(images,index) {
//gallery opations
  var dmGalleryOpations = {
    showButtons: true,
    canScale: true
  }
  window.hammers = {};
  window.hammers.dmGallery = new DmGallery(dmGalleryOpations);
  window.hammers.dmGallery.show(images,index);
  //transform_html = {scale: 1, lastScale: 1, top: 0, left: 0, lastTop:0, lastLeft:0};
  //requestElementUpdate_html();
  //hammerMask_html.style.display = "block";
  //hammerBg_html.style.display = "block";
  //
  //hammerImgs_html = images;
  //currentIndex_html = index;
  //changecss_html('.blur-tag','-webkit-filter','blur(30px)');
  //document.body.style.overflow = "hidden";
  //imgBig_html.src = hammerImgs_html[index];
  //imgNumIndex_html = hammerImgs_html.length - 1;
  //hammerPage_html.innerHTML = (currentIndex_html+1)+"/"+(imgNumIndex_html+1);

  window.hammerBgShow = true ;
}
function hideHammer(e) {
  //if(e.target.nodeName == "IMG" | e.target.nodeName == "SPAN") {
  //  return;
  //}
  //changecss_html('.unblur-tag','-webkit-filter','none');
  //document.body.style.overflow = "auto";
  //hammerMask_html.style.display = "none";
  //hammerBg_html.style.display = "none";
  window.hammers.dmGallery.hide();
  window.hammers = {};
  window.hammerBgShow = false ;
}


function changecss(theClass, element, value) {
    var cssRules;
    if (document.all) {
        cssRules = 'rules';
    } else if (document.getElementById) {
        cssRules = 'cssRules';
    }
    for (var S = 0; S < document.styleSheets.length; S++) {
        for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
            if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
                document.styleSheets[S][cssRules][R].style[element] = value;
                return ;
            }
        }
    }
}
